package com.dchh.android.suverynew.presenter.impl;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.dchh.android.suverynew.R;
import com.dchh.android.suverynew.common.SPreferencesManager;
import com.dchh.android.suverynew.module.http.ILoginModule;
import com.dchh.android.suverynew.module.http.impl.LoginModule;
import com.dchh.android.suverynew.presenter.ILoginPresenter;
import com.dchh.android.suverynew.view.ICommonView;
import com.dchh.android.suverynew.view.ILoginView;
import com.dhcc.android.base.base.MSApplication;
import com.dhcc.android.base.common.bean.User;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.StringUtil;

/**
 * Created by eric on 2016/8/6.
 */
public class LoginPresenter extends CommonPresenter implements ILoginPresenter, LoginModule.ILoginModuleListener {
    private ILoginView mLoginView;
    private ILoginModule mLoginModule;

    public LoginPresenter(ILoginView loginView) {
        super((ICommonView) loginView);
        this.mLoginView = loginView;
        this.mLoginModule = new LoginModule(this);
    }

    @Override
    public void updateVersion() {
        this.mLoginModule.updateVersion(this.mLoginView.getLastException());
    }

    @Override
    public void login() {
        if (StringUtil.isFastDoubleClick(500)) {
            return;
        }

        String userNameValue = mLoginView.getUsername();
        String passwordValue = mLoginView.getPassword();

        if (TextUtils.isEmpty(userNameValue)) { // 用户名为空
            mLoginView.onLoginUsernameCheck(MSApplication.instance().getString(R.string.username_null));
        } else if (userNameValue.contains(" ")) { // 用户名为存在空格
            mLoginView.onLoginUsernameCheck(MSApplication.instance().getString(R.string.error_invalid_user));
        } else if (TextUtils.isEmpty(passwordValue)) { // 密码为空
            mLoginView.onLoginPasswordCheck(MSApplication.instance().getString(R.string.password_null));
        } else if (passwordValue.contains(" ")) { // 密码存在空格
            mLoginView.onLoginPasswordCheck(MSApplication.instance().getString(R.string.error_invalid_password));
        } else {
            if (mLoginView.isNetWorkAvailable()) { // 判断是否联网
                mLoginModule.login();
            } else {
                User u = mLoginView.getDBManager().login(userNameValue, passwordValue);
                if (u == null) {
                    mLoginView.loginErrorNoUser();
                    StringUtil.dismissDialog(Constants.DIALOG);
                } else {
                    String f = "0";
                    if (mLoginView.isRemberUsername()) {
                        f = "1";
                    }
                    Constants.USERNAME = mLoginView.getUsername();
                    Constants.PASSWORD = mLoginView.getPassword();
                    mLoginView.getDBManager().addUser(Constants.USERNAME, Constants.PASSWORD);

                    SPreferencesManager.saveValue(MSApplication.ACCOUNT_ISAUTOLOGIN_KEY, f);
                    if (f.equals("1")) {
                        SPreferencesManager.saveValue(MSApplication.ACCOUNT_USERNAME_KEY, Constants.USERNAME);
                        SPreferencesManager.saveValue(MSApplication.ACCOUNT_PASSWORD_KEY, Constants.PASSWORD);
                    }
                    mLoginView.onLoginSUccess();
                }
            }
        }

    }

    @Override
    public void onShouldUpdate() {
        this.mLoginView.onNeedUpdate();
    }

    @Override
    public void onLastIsException() {
        this.mLoginView.onLastException();
    }

    @Override
    public void onLoginSUccess(JSONObject jsonObject) {
        String code = jsonObject.getString("RETCODE");
        String msga = jsonObject.getString("RETMSGA");
        if (code.equals(Constants.success)) {
            String f = "0";
            if (mLoginView.isRemberUsername()) {
                f = "1";
            }
            Constants.USERNAME = mLoginView.getUsername();
            Constants.PASSWORD = mLoginView.getPassword();
            mLoginView.getDBManager().addUser(Constants.USERNAME, Constants.PASSWORD);

            SPreferencesManager.saveValue(MSApplication.ACCOUNT_ISAUTOLOGIN_KEY, f);
            if (f.equals("1")) {
                SPreferencesManager.saveValue(MSApplication.ACCOUNT_USERNAME_KEY, Constants.USERNAME);
                SPreferencesManager.saveValue(MSApplication.ACCOUNT_PASSWORD_KEY, Constants.PASSWORD);
            }
            mLoginView.onLoginSUccess();
        } else if (code.equals(Constants.no_user)) {
            mLoginView.loginErrorNoUser();
        } else if (code.equals(Constants.too_long)) {
            mLoginView.toast("登录失败，请重试" + msga.toString());
        } else {
            mLoginView.toast("登录失败，请重试");
        }
    }
}